import com.alibaba.fastjson.JSONObject;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.Socket;
import java.net.UnknownHostException;
import java.util.Date;
import java.util.Scanner;

public class Client extends Thread {
    private TalkingRoom talkingRoom;

    //定义一个Socket对象
    Socket socket = null;

    public Client(String host, int port) {
        talkingRoom=new TalkingRoom();
        try {
            //需要服务器的IP地址和端口号，才能获得正确的Socket对象
            socket = new Socket(host, port);
            socket.getOutputStream().write(new String("TalkingRoom").getBytes("UTF-8"));
            talkingRoom.setSocket(socket);
            socket.getOutputStream().flush();
//            socket.getOutputStream().write(new String(talkingRoom.getName()).getBytes("UTF-8"));
        }  catch (IOException e) {
//            e.printStackTrace();
            if ("Connection refused: connect".equals(e.getMessage())){
                Message message=new Message();
                message.setName("系统");
                message.setMsg("服务器未连接，请稍后重试。");
                message.setDate(new Date());
                talkingRoom.GetMsg(message);
            }
        }

    }

    @Override
    public void run() {
        //客户端一连接就可以写数据个服务器了
//        new sendMessThread().start();
        super.run();
        try {
            // 读Sock里面的数据
            InputStream s = socket.getInputStream();
            byte[] buf = new byte[1024];
            int len = 0;
            while ((len = s.read(buf)) != -1) {
                System.out.println(new String(buf, 0, len,"UTF-8"));
                Message message= JSONObject.parseObject(new String(buf, 0, len,"UTF-8"),Message.class);
                System.out.println(message);
                talkingRoom.GetMsg(message);
            }

        } catch (IOException e) {
            //e.printStackTrace();
            if ("Connection reset".equals(e.getMessage())){
                Message message=new Message();
                message.setName("系统");
                message.setMsg("服务器已关闭，请联系管理员。");
                message.setDate(new Date());
                talkingRoom.setSocket(null);
                talkingRoom.GetMsg(message);
            }
        }
    }

    //往Socket里面写数据，需要新开一个线程
    class sendMessThread extends Thread{
        @Override
        public void run() {
            super.run();
            //写操作
            Scanner scanner=null;
            OutputStream os= null;
            try {
                scanner=new Scanner(System.in);
                os= socket.getOutputStream();
                String in="";
                do {
                    in=scanner.next();
                    os.write((""+in).getBytes());
                    os.flush();
                } while (!in.equals("bye"));
            } catch (IOException e) {
                e.printStackTrace();
            }
            scanner.close();
            try {
                os.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
    //函数入口
    public static void main(String[] args) {
        //需要服务器的正确的IP地址和端口号
//        Client clientTest=new Client("127.0.0.1", 8888);
        Client clientTest=new Client("47.103.200.210", 8888);
        clientTest.start();
    }
}